/**
  ******************************************************************************
  * @file    USART_app.c 
  * @author  Thongnh
  * @version V1.0
  * @date    18-07-2011
  * @brief   Main process of USART
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, XICUTELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 Xicutelectronics</center></h2>
  ******************************************************************************
  */ 

#include "includes.h"

#ifdef USE_PC

/* Private variables ---------------------------------------------------------*/
// uint16_t    CmdBuff[16];
// uint8_t     m_Size     = 16;
// uint8_t     m_Count    = 0;
// uint8_t     m_Head     = 0;
// uint8_t     m_Tail     = 0;

/*******************************************************************************
Function name: PC_AppInit
Decription: This function initialize USB
Input: None
Output: None
*******************************************************************************/
void PC_AppInit(void)
{
    PCDriverInit();
}

/*******************************************************************************
Function name: PCApp_PushCmd
Decription: This function initialize USB
Input: cmd -> command send to PC
Output: None
*******************************************************************************/
// bool PCApp_PushCmd(uint16_t cmd)
// {
    // if ( m_Count == m_Size ) { return( FALSE ); }

    // CmdBuff[ m_Tail++ ] = cmd;
    // if ( m_Tail == m_Size ) { m_Tail = 0; }
    // ++m_Count;

    // PCAppCmd = cmd;

    // return( TRUE );
// }


/*******************************************************************************
Function name: PCApp_PushCmd
Decription: This function initialize USB
Input: cmd -> command send to PC
Output: None
*******************************************************************************/
// uint16_t PCApp_PopCmd(void)
// {
    // static uint16_t CmdOld = STA_NOTHING;
    // uint16_t Element = STA_NOTHING;
    // if ( 0 != m_Count )
    // {
        // Element = CmdBuff[ m_Head++ ];
        // if ( m_Head == m_Size ) { m_Head = 0; }
        // --m_Count;
    // }
    // //return( Element );
    // //if (g_flag_idle == TRUE) PCAppCmd = STA_SAMPLE_READY;
    // return   PCAppCmd;
// }


void PC_AdjustTime(uint8_t *pc_time)
{
}

/*******************************************************************************
Function name: PC_Execute
Decription: This function execute command incomming packed
Input: None
Output: None
*******************************************************************************/
void PC_Execute(uint8_t *data, uint16_t len)
{
    switch (data[0])
    {
        case CMD_POLLING:
        {
            data[0] = 'A';
            *((uint16_t *)&data[1]) = GetFrequency();
            PCDriverSendPacked(data, 3);
        }
        break;
        
        case CMD_GET_TIME:
        {
            data[0] = 'A';
            *((uint16_t *)&data[1]) = GetFrequency();
            PCDriverSendPacked(data, 3);
        }
        break;
        
        case CMD_SET_TIME:
        {
            SetFrequency(*((uint16_t *)&data[1]));
            data[0] = 'A';
            PCDriverSendPacked(data, 1);
        }
        break;
        
        default:
            data[0] = 'N';
            data[1] = COMMAND_NOT_SUPPORT;
            PCDriverSendPacked(data, 2);
        break;
    }
}

/*******************************************************************************
Function name: PC_AppProcess
Decription: This function Process all income packed or out packed of USART1
Input: None
Output: None
*******************************************************************************/
void PC_AppProcess(void)
{
    uint8_t data[IN_REPORT_SIZE];
    uint8_t length;

    length = PCDriverCheckPackedIncome(data);
    if (length > 0)
    {
        PC_Execute(data, length);
    }
    
}

#endif

// end of file

